package com.zpark.dao.impl;

import com.zpark.dao.StudentDao;
import com.zpark.entity.Student;
import com.zpark.utils.DaoUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    @Override
    public void insert(Student student) {
        String sql = "INSERT INTO student (username, password, name, age, address, phone, email, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            DaoUtil.executeUpdate(sql, student.getUsername(), student.getPassword(), student.getName(), student.getAge(), student.getAddress(), student.getPhone(), student.getEmail(), student.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Integer id, Student student) {
        String sql = "UPDATE student SET username=?, password=?, name=?, age=?, address=?, phone=?, email=?, status=? WHERE id=?";
        try {
            DaoUtil.executeUpdate(sql, student.getUsername(), student.getPassword(), student.getName(), student.getAge(), student.getAddress(), student.getPhone(), student.getEmail(), student.getStatus(), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        String sql = "DELETE FROM student WHERE id=?";
        try {
            DaoUtil.executeUpdate(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Student selectById(Integer id) {
        String sql = "SELECT * FROM student WHERE id=?";
        try (ResultSet rs = DaoUtil.executeQuery(sql, id)) {
            if (rs.next()) {
                return mapResultSetToStudent(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Student> selectPage(Integer size, Integer number) {
        String sql = "SELECT * FROM student LIMIT ?,?";
        List<Student> students = new ArrayList<>();
        try (ResultSet rs = DaoUtil.executeQuery(sql,number,size)) {
            while (rs.next()) {
                students.add(mapResultSetToStudent(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;
    }

    @Override
    public Student selectByUsernameAndPassword(String username, String password) {
        String sql = "SELECT * FROM student WHERE username=? and password =?";
        try (ResultSet rs = DaoUtil.executeQuery(sql, username,password)) {
            if (rs.next()) {
                return mapResultSetToStudent(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Student mapResultSetToStudent(ResultSet rs) throws SQLException {
        Student student = new Student();
        student.setId(rs.getInt("id"));
        student.setUsername(rs.getString("username"));
        student.setPassword(rs.getString("password"));
        student.setName(rs.getString("name"));
        student.setAge(rs.getInt("age"));
        student.setAddress(rs.getString("address"));
        student.setPhone(rs.getString("phone"));
        student.setEmail(rs.getString("email"));
        student.setStatus(rs.getInt("status"));
        return student;
    }
}
